package main

import (
	"fmt"
	"crypto/sha256"
)

func isValidHashDifficulty(h string, difficulty int) bool  {

	b := len(h) // 64
	var i int
	for i = 0 ; i < b; i++  {
		if  h[i] != '0'{
			break
		}
	}

	return i >= difficulty
}


/*
let nonce = 0;
let hash;
let input;
while(!isValidHashDifficulty(hash)) {
  nonce = nonce + 1;
  input = index + previousHash + timestamp + data + nonce;
  hash = CryptoJS.SHA256(input)
}
*/

func main()  {
	h := "0000e1343c8d9ec8a8996f0c1c8d1f9f1d954a750c3db5525205f401c516222d"
	fmt.Println(isValidHashDifficulty(h,4))

	nonce := 0
	hashString := ""

	shaObj := sha256.New()

	fmt.Printf("%x\n", )

	preHash := "0000fedaa499741317a18f1ad626f933776ad24822cb422634978bfe8005c94b"
	for !isValidHashDifficulty(hashString,6)  {
		nonce++
		input := "100" + preHash + "Thu, 26 Apr 2018 03:30:54 GMT" + "A -> B 100" + string(nonce)
		shaObj.Write([]byte(input))
		hashString = fmt.Sprintf("%x",shaObj.Sum(nil));
		fmt.Println(hashString)
	}


}
